_package("alz.core");

/**
 * 滚动条动画
 */
_class("Animation", "", function(){
	this._init = function(agent, func){
		_super._init.call(this);
		this._agent = agent;
		this._func = func;
		this.fps    = 20;  //帧率
		this.maxFps = 20;  //最大帧率
		this.timer  = 0;   //定时器
		this.framesHandled = 0;
		this.startTime = 0;
		this.endTime   = 0;
		this.processingTime = 0;
	};
	this.getFrameDuration = function(){
		return 1000 / this.fps;
	};
	this.getLastActualFps = function(){
		var duration = (this.endTime - this.startTime - this.processingTime) / 1000;
		return this.framesHandled / duration;
	};
	this.start = function(){
		this.startTime = new Date().getTime();
		this.framesHandled = 0;
		this.timer = runtime.startTimer(this.getFrameDuration(), this, "play");
		this.processingTime = 0;
	};
	this.stop = function(){
		runtime.stopTimer(this.timer);
		this.endTime = new Date().getTime();
		var lastFps = this.getLastActualFps();
		if(!this.maxFps || lastFps > this.maxFps){
			this.maxFps = lastFps;
		}
		if(this.autoAdjustFps){
			this.fps = this.maxFps;
		}
	};
	this.play = function(){
		this.timer = runtime.startTimer(this.getFrameDuration(), this, "play");
		var currentTime = new Date().getTime();
		//------------------------
		this._func.apply(this._agent);
		//------------------------
		this.framesHandled++;
		this.processingTime += new Date().getTime() - currentTime;
	};
});